import {
  Body,
  Controller,
  Request,
  Get,
  Param,
  ParseIntPipe,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import { CommentService } from './comment.service';
import { CreateCommentDto, QueryCommentListDto } from '@/modules/comment/dto/dto';
import { AuthGuard } from '@nestjs/passport';

@Controller('comment')
export class CommentController {
  constructor(private readonly commentService: CommentService) {}

  // 发送评论
  @Post('send')
  @UseGuards(AuthGuard('jwt'))
  async sendComment(@Body() createCommentDto: CreateCommentDto, @Request() req: any) {
    return this.commentService.createComment(createCommentDto, req.user.userId);
  }

  // 按文章id获取评论列表
  @Get('list/:id')
  async queryCommentList(
    @Param('id', ParseIntPipe) id: number,
    @Query() query: QueryCommentListDto,
  ) {
    return this.commentService.queryCommentList(id, query);
  }

  // 按父评论id获取子评论列表
  @Get('children/:id')
  async queryChildrenCommentList(
    @Param('id', ParseIntPipe) id: number,
    @Query() query: QueryCommentListDto,
  ) {
    return this.commentService.queryChildrenCommentList(id, query);
  }

  // 获取扁平的评论列表
  @UseGuards(AuthGuard('jwt'))
  @Get('flat')
  async queryFlatCommentList(@Query() query: QueryCommentListDto) {
    return this.commentService.queryFlatCommentList(query);
  }

  // 删除评论
  @UseGuards(AuthGuard('jwt'))
  @Post('delete/:id')
  async deleteComment(@Param('id', ParseIntPipe) id: number) {
    return this.commentService.deleteComment(id);
  }
}
